//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  MeetingBoard.m
//  iHeima
//
//  Created by he chao on 5/27/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "MeetingBoard.h"
#import "MeetingCell.h"
#import "MeetingCreateBoard.h"
#import "LoginBoard.h"
#import "MeetingWaitBoard.h"

#pragma mark -

@interface MeetingBoard()
{
	//<#@private var#>
}
@end

@implementation MeetingBoard
DEF_SIGNAL(ADD_IN)

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"私董会";
    self.navigationController.navigationBarHidden = NO;
    [self showBarButton:BeeUINavigationBar.RIGHT image:IMAGESTRING(@"navi_meeting")];
    [self showNaviBar];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, self.viewHeight-50-(IOS7_OR_LATER?64:0))];
    MeetingBoard *board = self;
    [myTableView addPullToRefreshWithActionHandler:^{
        [board getBoardList];
    }];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
    
    [self getBoardList];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
    MeetingCreateBoard *board = [[MeetingCreateBoard alloc] init];
    board.view.frame = CGRectZero;
    [[MainBoard sharedInstance].navigationController pushViewController:board animated:YES];
    //[[MainBoard sharedInstance].stack pushBoard:[[MeetingCreateBoard alloc] init] animated:YES];
    //[[MainBoard sharedInstance].stack pushBoard:[[MeetingCreateBoard alloc] init] animated:YES];
}

ON_SIGNAL2(MeetingBoard,signal){
    if ([signal is:MeetingBoard.ADD_IN]) {
        
    }
}

- (void)getBoardList{
    BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apichat/listboard",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]);
    request.tag = 9527;
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        if (myTableView) {
            [myTableView.pullToRefreshView stopAnimating];
            [myTableView.infiniteScrollingView stopAnimating];
        }
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        if (myTableView) {
            [myTableView.pullToRefreshView stopAnimating];
            [myTableView.infiniteScrollingView stopAnimating];
        }
        [[BeeUITipsCenter sharedInstance] dismissTips];
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"%@",json);
        switch ([json[@"status"] intValue]) {
            case 1:
            {
                if (request.tag == 9527) {
                    arrayMeeting = json[@"res"];
                    [myTableView reloadData];
                }
                else if (request.tag == 9528) {
                    [self getBoardList];
                    
                    MeetingWaitBoard *board = [[MeetingWaitBoard alloc] init];
                    board.isAdmin = NO;
                    board.dictMeeting = dictSelMeeting;
                    [[MainBoard sharedInstance].stack pushBoard:board animated:YES];
                }
                
            }
                break;
                
            default:
                [[BeeUITipsCenter sharedInstance] presentMessageTips:json[@"info"]];
                break;
        }
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return arrayMeeting.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeetingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[MeetingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        //cell.selIndex = self.selIndex;
        [cell initSelf];
    }
    
    cell.dictMeeting = arrayMeeting[indexPath.row];
//    cell.dictActivity = arrayActivities[indexPath.row+1];
//    cell.backgroundColor = [UIColor clearColor];
//    UIView *vi = [[UIView alloc] init];
//    vi.backgroundColor = RGB(242, 242, 242);
//    cell.backgroundView = vi;
//    
//    UIView *viSelected = [[UIView alloc] init];
//    viSelected.backgroundColor = [UIColor whiteColor];
//    cell.selectedBackgroundView = viSelected;
    
    [cell load];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dictMeeting = arrayMeeting[indexPath.row];
    if ([dictMeeting[@"showtype"] intValue]==1 || [dictMeeting[@"showtype"] intValue]==2) {
        [[BeeUITipsCenter sharedInstance] presentLoadingTips:@"正在加入..."];
        dictSelMeeting = dictMeeting;
        BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apichat/joinboard",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",dictMeeting[@"id"]);
        request.tag = 9528;
    }
}


@end
